import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import base from "@src/store/base";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['barHeight']
  }],
  properties: {
    storeList: {
      type: Object,
      value: []
    },
    categoryTree: {
      type: Object,
      value: []
    },
    storeId: null,
    storeName: null,
  },
  data: {
    showBg: false,
    searchStr: '',
    filterList: [{
      id: "category",
      title: "全部类别",
      type: "multi",
      data: []
    }, {
      id: "sort",
      title: "智能排序",
      type: "single",
      data: [
        { id: 0, title: "智能排序" },
        { id: 1, title: "价格从低到高" },
        { id: 2, title: "价格从高到低" }
      ]
    }, {
      id: "buyWay",
      title: "购买方式",
      type: "multi",
      data: []
    }],
    initForm: {}
  },
  lifetimes: {},
  observers: {
    'storeList': function (storeList) {
      if (!storeList) {
        return true;
      }
      let filterList = this.data.filterList
      filterList[2].data = this.parseStoreDelivery(storeList)
      this.setData({ filterList })
    },
    'categoryTree': function (categoryTree) {
      if (!categoryTree) {
        return true;
      }
      let filterList = this.data.filterList
      filterList[0].data = this.parseCategoryData(categoryTree)
      this.setData({ filterList })
    },
    'storeId,storeName': function (storeId, storeName) {
      if (!storeId) {
        return;
      }
      let initForm = {}
      initForm['buyWay'] = {
        value: ["", Number(storeId)],
        title: storeName
      }
      this.setData({ initForm })
    }
  },
  methods: {
    parseCategoryData(categoryList) {
      let tempData = categoryList;
      tempData.unshift({ id: 0, title: "全部", children: [] })
      tempData.forEach(item => {
        if (item.children && item.children.length > 0 && item.id !== 0) {
          let arr = item.children;
          arr.unshift({ id: item.id, parentId: item.id, title: "全部" + item.title });
        }
      })
      return tempData;
    },

    parseStoreDelivery(storeList) {
      let expressList = [{ "id": "", "title": "全部店铺", "subTitle": "直邮到家" }],
        localList = [{ "id": "", "title": "全部店铺", "subTitle": "同城配送" }],
        selfPickupList = [{ "id": "", "title": "全部店铺", "subTitle": "到店自提" }],
        allList = [{ "id": "", "title": "全部店铺" }]
        ;

      for (let i = 0; i < storeList.length; i++) {
        let store = storeList[i];
        let item = { "id": store.storeId, "title": store.name }
        allList.push(item)
        if (store.delivery.supplyExpress) {
          expressList.push(item)
        }
        if (store.delivery.supplyLocal) {
          localList.push(item)
        }
        if (store.delivery.supplySelfPickup) {
          selfPickupList.push(item)
        }
      }
      return [{
        "id": "",
        "title": "全部",
        "children": allList
      }, {
        "id": "EXPRESS",
        "title": "直邮到家",
        "children": expressList
      }, {
        "id": "LOCAL",
        "title": "同城配送",
        "children": localList
      }, {
        "id": "SELF_PICKUP",
        "title": "到店自提",
        "children": selfPickupList
      }]
    },
    handleFilterStatus(e) {
      this.setData({ showBg: !!e.detail })
    },
    handleFilterChange(e) {
      let filterForm = e.detail;
      this.triggerEvent('filter', filterForm)
    },
    handleSearch(e) {
      let keywords = e.detail;
      console.info("e", e)
      this.triggerEvent('search', keywords)
    },
    handleStop() {
      return true;
    },
    handleClose() {
      this.selectComponent("#goodsFilter").close();
    },
    cancelInput() {
      this.setData({ 'searchStr': '' })
      this.triggerEvent('search', '')
    }
  }
});
